import 'package:flutter/material.dart';

/// 垂直方向渐变色容器组件
class VerticalGradientContainer extends StatelessWidget {
  final Widget child;
  final List<Color> colors;
  final List<double>? stops;
  final AlignmentGeometry begin;
  final AlignmentGeometry end;
  final TileMode tileMode;
  final GradientTransform? transform;
  final BorderRadius? borderRadius;
  final BoxShape shape;
  final EdgeInsetsGeometry? padding;
  final EdgeInsetsGeometry? margin;

  /// 垂直方向渐变色容器
  ///
  /// [child] 子组件
  /// [colors] 渐变颜色列表，至少需要两种颜色
  /// [stops] 渐变位置列表，长度必须与 colors 相同
  /// [begin] 渐变开始位置，默认顶部中间
  /// [end] 渐变结束位置，默认底部中间
  /// [tileMode] 渐变平铺模式
  /// [transform] 渐变变换
  /// [borderRadius] 边框圆角
  /// [shape] 形状
  /// [padding] 内边距
  /// [margin] 外边距
  const VerticalGradientContainer({
    Key? key,
    required this.child,
    required this.colors,
    this.stops,
    this.begin = Alignment.topCenter,
    this.end = Alignment.bottomCenter,
    this.tileMode = TileMode.clamp,
    this.transform,
    this.borderRadius,
    this.shape = BoxShape.rectangle,
    this.padding,
    this.margin,
  })  : assert(colors.length >= 2, '渐变颜色至少需要两种'),
        super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: margin,
      padding: padding,
      decoration: BoxDecoration(
        gradient: LinearGradient(
          colors: colors,
          stops: stops,
          begin: begin,
          end: end,
          tileMode: tileMode,
          transform: transform,
        ),
        borderRadius: shape == BoxShape.rectangle ? borderRadius : null,
        shape: shape,
      ),
      child: child,
    );
  }
}

/// 简易垂直方向渐变色容器组件
class SimpleVerticalGradientContainer extends StatelessWidget {
  final Widget child;
  final Color startColor;
  final Color endColor;
  final BorderRadius? borderRadius;
  final BoxShape shape;
  final EdgeInsetsGeometry? padding;
  final EdgeInsetsGeometry? margin;

  /// 简易垂直方向渐变色容器
  ///
  /// [child] 子组件
  /// [startColor] 起始颜色
  /// [endColor] 结束颜色
  /// [borderRadius] 边框圆角
  /// [shape] 形状
  /// [padding] 内边距
  /// [margin] 外边距
  const SimpleVerticalGradientContainer({
    Key? key,
    required this.child,
    required this.startColor,
    required this.endColor,
    this.borderRadius,
    this.shape = BoxShape.rectangle,
    this.padding,
    this.margin,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return VerticalGradientContainer(
      colors: [startColor, endColor],
      borderRadius: borderRadius,
      shape: shape,
      padding: padding,
      margin: margin,
      child: child,
    );
  }
}

/// 水平方向渐变色容器组件
class HorizontalGradientContainer extends StatelessWidget {
  final Widget child;
  final List<Color> colors;
  final List<double>? stops;
  final AlignmentGeometry begin;
  final AlignmentGeometry end;
  final TileMode tileMode;
  final GradientTransform? transform;
  final BorderRadius? borderRadius;
  final BoxShape shape;
  final EdgeInsetsGeometry? padding;
  final EdgeInsetsGeometry? margin;

  /// 水平方向渐变色容器
  ///
  /// [child] 子组件
  /// [colors] 渐变颜色列表，至少需要两种颜色
  /// [stops] 渐变位置列表，长度必须与 colors 相同
  /// [begin] 渐变开始位置，默认左侧中间
  /// [end] 渐变结束位置，默认右侧中间
  /// [tileMode] 渐变平铺模式
  /// [transform] 渐变变换
  /// [borderRadius] 边框圆角
  /// [shape] 形状
  /// [padding] 内边距
  /// [margin] 外边距
  const HorizontalGradientContainer({
    Key? key,
    required this.child,
    required this.colors,
    this.stops,
    this.begin = Alignment.centerLeft,
    this.end = Alignment.centerRight,
    this.tileMode = TileMode.clamp,
    this.transform,
    this.borderRadius,
    this.shape = BoxShape.rectangle,
    this.padding,
    this.margin,
  })  : assert(colors.length >= 2, '渐变颜色至少需要两种'),
        super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: margin,
      padding: padding,
      decoration: BoxDecoration(
        gradient: LinearGradient(
          colors: colors,
          stops: stops,
          begin: begin,
          end: end,
          tileMode: tileMode,
          transform: transform,
        ),
        borderRadius: shape == BoxShape.rectangle ? borderRadius : null,
        shape: shape,
      ),
      child: child,
    );
  }
}

/// 简易水平方向渐变色容器组件
class SimpleHorizontalGradientContainer extends StatelessWidget {
  final Widget child;
  final Color startColor;
  final Color endColor;
  final BorderRadius? borderRadius;
  final BoxShape shape;
  final EdgeInsetsGeometry? padding;
  final EdgeInsetsGeometry? margin;

  /// 简易水平方向渐变色容器
  ///
  /// [child] 子组件
  /// [startColor] 起始颜色
  /// [endColor] 结束颜色
  /// [borderRadius] 边框圆角
  /// [shape] 形状
  /// [padding] 内边距
  /// [margin] 外边距
  const SimpleHorizontalGradientContainer({
    Key? key,
    required this.child,
    required this.startColor,
    required this.endColor,
    this.borderRadius,
    this.shape = BoxShape.rectangle,
    this.padding,
    this.margin,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return HorizontalGradientContainer(
      colors: [startColor, endColor],
      borderRadius: borderRadius,
      shape: shape,
      padding: padding,
      margin: margin,
      child: child,
    );
  }
}
